<template>
	<view class="container">
		<view class="">

		</view>
	</view>
</template>

<script>
	const isAgreementConfirmed = uni.getStorageSync('isAgreementConfirmed')
	export default {
		data() {
			return {

			}
		},
		onLoad() {
			uni.showLoading({
				title: '加载中...'
			})
			this.login().then((code) => {
				console.log('code', code);
				return this.$cloud.callFunction({
					name: 'login',
					data: {
						code
					}
				})
			}).then((res) => {
				uni.hideLoading()
				console.log(res);
				if (res.result.status !== 0) {
					return Promise.reject(new Error(res.result.msg))
				}
				uni.setStorageSync('token', res.result.token)
				if (isAgreementConfirmed) {
					uni.redirectTo({
						url: '/pages/report/report'
					})
				} else {
					uni.redirectTo({
						url: '/pages/agreement/agreement'
					})
				}
			}).catch((e) => {
				uni.hideLoading()
				uni.showModal({
					content: '出现错误，请稍后再试',
					showCancel: false
				})
			})
		},
		methods: {
			login() {
				return new Promise((resolve, reject) => {
					uni.login({
						provider: 'weixin',
						success(e) {
							if (e.code) {
								resolve(e.code)
							} else {
								reject(new Error('微信登录失败'))
							}
						},
						fail(e) {
							reject(new Error('微信登录失败'))
						}
					})
				})
			}
		}
	}
</script>

<style>

</style>
